<?php

class directoLoadTask extends sfBaseTask
{
  //protected $c_attributes = array('code', 'name', 'discount_price', 'weight'); // if you change this, change function compareInputDataWithDB
  //protected $xml_file_from_server;
  
  protected function configure()
  {
    // // add your own arguments here
     $this->addArguments(array(
      new sfCommandArgument('command', sfCommandArgument::OPTIONAL, 'Import data from server to DB use all, last', 'last'),
      //new sfCommandArgument('filename', sfCommandArgument::REQUIRED, 'invoice filename'),
    ));
      
    $this->addOptions(array(
      new sfCommandOption('application', null, sfCommandOption::PARAMETER_REQUIRED, 'The application name'),
      new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'),
      new sfCommandOption('connection', null, sfCommandOption::PARAMETER_REQUIRED, 'The connection name', 'doctrine'),
      // add your own options here
    ));

    $this->namespace        = 'directo';
    $this->name             = 'load';
    $this->briefDescription = '';
    $this->detailedDescription = <<<EOF
The [directo:load|INFO] task does things.
Call it with:

  [php symfony [directo:load|INFO]
EOF;
  }

  protected function execute($arguments = array(), $options = array())
  {
    // initialize the database connection
    $databaseManager = new sfDatabaseManager($this->configuration);
    $connection = $databaseManager->getDatabase($options['connection'])->getConnection();
	
   switch( $arguments['command'] )
   {
       case 'allProducts':
           $this->logSection("Info(all)", "Connect to server");           
           
           $service = new DirectoWebService();
           $request = $service->getRequest( 'products', array() );
           $response = $request->send();
           
           if(!($response instanceof SimpleWebResponse))
           {
                $this->logSection("Error", 'Empty data from server');
                exit;
           }

           $mapper = new ProductCategoriesMapper($response);
                      
           $this->logSection("Info", 'Transfer data...');
           $this->logSection("Info", 'It can take several minutes');
           
           $dataArray = $mapper->writeProductsInDB('all');
           
           $this->logSection("Info", 'Added '.$dataArray[0].' rows, Updated '.$dataArray[1].' rows' );
           
           $this->logSection("Info", 'Transfer data...');
           
           $deletedRows = $mapper->compareXMLDataAndDataInDB();
           
           $this->logSection("Info", 'Deleted '.$deletedRows.' rows');
           $this->logSection("End", 'End');  
       break;         
       case 'lastProducts':
           $this->logSection("Info(last)", "Connect to server");
           
           $service = new DirectoWebService();
           $request = $service->getRequest( 'products', array() );
           $response = $request->send();
           
           if(!($response instanceof SimpleWebResponse))
           {
                $this->logSection("Error", 'Empty data from server');
                exit;
           }
           
           $this->logSection("Info", 'Transfer data...');
                                 
           $mapper = new ProductCategoriesMapper($response);
                      
           $dataArray = $mapper->writeProductsInDB('last');
           
           $this->logSection("Info", 'Added '.$dataArray[0].' rows, Updated '.$dataArray[1].' rows' );
           $this->logSection("End", 'End');  
       break;
       case 'allProductGroups':
           $this->logSection("Info(all)", "Connect to server");           
           
           $service = new DirectoWebService();
           $request = $service->getRequest( 'productGroup', array() );
           $response = $request->send();
           
           if(!($response instanceof SimpleWebResponse))
           {
                $this->logSection("Error", 'Empty data from server');
                exit;
           }

           $mapper = new ProductCategoriesMapper($response);
                      
           $this->logSection("Info", 'Transfer data...');
           $this->logSection("Info", 'It can take several minutes');
           
           $dataArray = $mapper->writeProductGroupsInDB();
           
       break;    
       case 'cleanDB':
       break;       
       default:
           $this->logSection("Error", "Invalid inserted arguments");
           $this->logSection("End", 'End');  
           exit();           
    }
   } 
   
}
